//
//  NIMenuView.m
//  gweAD
//
//  Created by navinfo air on 2016/10/31.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "NIMenuView.h"
#import "TagDefines.h"

@implementation NIMenuView{
    
    __weak IBOutlet UITableView *menuTableView;
    __weak IBOutlet UIView *contentView;
    
    float cellHeight;
    
    __weak IBOutlet UIButton *bgButton;
    __weak IBOutlet UIView *menuHeaderView;
    __weak IBOutlet UIButton *accountButton;
    NSArray *menuItemArray;
    NSArray *menuItemImageNameArray;
    __weak IBOutlet UILabel *indicationLabel;
    
    
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
    
    menuItemArray = @[@"车辆管理",@"客服电话",@"在线客服",@"关于"];
    menuItemImageNameArray = @[@"menu_icon_car",@"menu_icon_telephone",@"menu_icon_opinion",@"menu_icon_about"];
    
    
    menuTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, menuTableView.frame.size.width, 0)];
    menuTableView.backgroundColor = [UIColor redColor];
    cellHeight = [[UIApplication sharedApplication] keyWindow].bounds.size.width/7;
    CGRect frame = menuHeaderView.frame;
    frame.size.height = cellHeight * 6;
    menuHeaderView.frame = frame;
    
    menuTableView.tableHeaderView = menuHeaderView;
    menuTableView.separatorColor = [UIColor redColor];
}

- (void)showMenu:(BOOL)show animate:(BOOL)animate{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    NSArray *viewTags = nil;
//    = @[@TAG_NOTIFICATIONINWINDOW,@TAG_MENUINWINDOW,@TAG_SECURITYPASSWORDINWINDOW,@TAG_SECURITYPASSWORDBGINWINDOW,@TAG_LOADINGINWINDOW];
    NSNumber *number;
    for (int i = 0; i < viewTags.count; i++) {
        number = [viewTags objectAtIndex:i];
        if ([window viewWithTag:number.integerValue]) {
            [window bringSubviewToFront:[window viewWithTag:number.integerValue]];
        }
    }
    
    
    
    if (!show) {
        bgButton.alpha = 0;
    }else{
        [self updateIndicationLabel];
        [menuTableView reloadData];
    }
    
    if (!animate) {
        if (!show) {
            self.hidden = YES;
        }
        CGRect frame = self.frame;
        if (show) {
            self.hidden = NO;
            frame.origin.x = 0;
            bgButton.alpha = 0.6;
        }else{
            frame.origin.x = - frame.size.width;
        }
        self.frame = frame;
        return;
    }
    
    [UIView animateWithDuration:0.5f animations:^{
        CGRect frame = self.frame;
        if (show) {
            self.hidden = NO;
            frame.origin.x = 0;
            bgButton.alpha = 0.6;
        }else{
            frame.origin.x = - frame.size.width;
        }
        self.frame = frame;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (!show) {
            self.hidden = YES;
        }
    }];
}

- (IBAction)onAccountButtonClicked:(id)sender {
    [self showMenu:NO animate:NO];
}


- (IBAction)onCloseButtonClick:(id)sender {
    [self showMenu:NO animate:YES];
}

- (void)setVehicleImage:(UIImage *)vehicleImage{
    _vehicleImage = vehicleImage;
    [menuTableView reloadData];
}

- (void)setLicenseNumber:(NSString *)licenseNumber{
    _licenseNumber = licenseNumber;
    [menuTableView reloadData];
}

#pragma mark - UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delegate) {
        [self.delegate menuDidSelect:indexPath.row];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuCellID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MenuCellID"];
    }
    [cell.imageView setImage:[UIImage imageNamed:[menuItemImageNameArray objectAtIndex:indexPath.row]]];
    cell.textLabel.text = [menuItemArray objectAtIndex:indexPath.row];
    
    cell.textLabel.font = [UIFont systemFontOfSize:17];
    cell.textLabel.textColor = [UIColor whiteColor];
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    //车牌号
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = _licenseNumber;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:17];
        cell.detailTextLabel.textColor = [UIColor redColor];
    }

    
    
    if (indexPath.row == 1 && ![cell.contentView viewWithTag:2002]) {
        UILabel *accLabel = [[UILabel alloc] init];
        accLabel.font = [UIFont systemFontOfSize:17];
        accLabel.backgroundColor =[UIColor clearColor];
        accLabel.textColor = [UIColor redColor];
        accLabel.text = @"400-666-6677";
        [accLabel sizeToFit];
        cell.accessoryView = accLabel;
        [accLabel setTag:2002];
    }else{
        UIImageView *accessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_system_arrow"]];
        cell.accessoryView = accessoryImageView;
        
    }
    
    
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)updateIndicationLabel{
//    AppContext *appContext = [AppContext getInstance];
//    if(!appContext.userModel.emergencyName || [appContext.userModel.emergencyName isEqualToString:@""] || [appContext.userModel.emergencyPhone isEqualToString:appContext.userModel.userAccount]){
//        indicationLabel.text = @"•";
//        indicationLabel.font = UIFontPingFangRegular(36);
//        indicationLabel.textColor = [UIColor redColor];
//    }else{
//        indicationLabel.text = @"";
//    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [menuItemArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cellHeight;
}


@end

